package com.studyhub.question.service.impl;

import com.studyhub.question.mapper.MailboxMapper;
import com.studyhub.question.mapper.QuestionMapper;
import com.studyhub.question.model.entity.QueAnswerEntity;
import com.studyhub.question.model.entity.QueMailboxEntity;
import com.studyhub.question.model.entity.QueQuestionEntity;
import com.studyhub.question.service.MailboxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 消息通知服务实现类
 */
@Service
@RequiredArgsConstructor
public class MailboxServiceImpl implements MailboxService {
    
    private final MailboxMapper mailboxMapper;
    private final QuestionMapper questionMapper;
    
    @Override
    @Transactional
    public void sendAnswerNotification(QueAnswerEntity answer) {
        // 查询问题信息
        QueQuestionEntity question = questionMapper.selectById(answer.getQuestionId());
        if (question == null) {
            return;
        }
        
        // 给问题作者发送通知（如果回答者不是问题作者）
        if (!answer.getUserId().equals(question.getUserId())) {
            QueMailboxEntity notification = new QueMailboxEntity();
            notification.setUserId(question.getUserId());
            notification.setQuestionId(answer.getQuestionId());
            notification.setAnswerId(answer.getAnswerId());
            notification.setContent("您的问题《" + question.getTitle() + "》收到了新的回答");
            notification.setCreateTime(new Date());
            notification.setIsRead(0);
            
            mailboxMapper.insert(notification);
        }
        
        // 如果是回复某个答案，给被回复者发送通知
        // 这里简化处理，实际项目中需要查询父答案的用户信息
        if (answer.getParentAnswerId() != null) {
            // 暂时跳过父答案通知，避免循环依赖
            // 实际项目中可以通过事件机制或者重构依赖关系来解决
        }
        
        // 如果@了某个用户，给被@用户发送通知
        if (answer.getAtUserId() != null && !answer.getUserId().equals(answer.getAtUserId())) {
            QueMailboxEntity notification = new QueMailboxEntity();
            notification.setUserId(answer.getAtUserId());
            notification.setQuestionId(answer.getQuestionId());
            notification.setAnswerId(answer.getAnswerId());
            notification.setContent("您在问题《" + question.getTitle() + "》中被@了");
            notification.setCreateTime(new Date());
            notification.setIsRead(0);
            
            mailboxMapper.insert(notification);
        }
    }
    
    @Override
    public Integer getUnreadCount(Integer userId) {
        return mailboxMapper.countUnreadMessages(userId);
    }
    
    @Override
    public List<QueMailboxEntity> getUserMessages(Integer userId, Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return mailboxMapper.selectUserMessages(userId, offset, pageSize);
    }
    
    @Override
    @Transactional
    public void markAsRead(Long mailboxId) {
        mailboxMapper.markAsRead(mailboxId);
    }
}
